<?php

//require_once("interfaces/FuncionarioDao.php");

class FuncionarioDaoImp {

    //armazena a conex�o com o BD
    private $conn;

    public function __construct() {
        include_once("Conexao.class.php");
        $funcCon = new Conexao;
        $this->conn = $funcCon->conectar();
    }

    public function inserir($func) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->exec("Insert into funcionario values(default,'{$func->getNome()}','{$func->getCpf()}',{$func->getRg()},'{$func->getEndereco()}','{$func->getLogin()}','{$func->getSenha()}','{$func->getTelefone1()}','{$func->getTelefone2()}',{$func->getAdministrador()}, {$func->getGerente()}, {$func->getAgente()})");
            $x = $this->buscarCpf($func->getCpf())->fetch(PDO::FETCH_OBJ);
            $func->setId($x->idFuncionario);
            //insere na outra tabela

            if ($result) {
                foreach ($func->getDepartamento() as $dep) {
                    $result = $this->conn->exec("Insert into funcionario_has_departamento values({$func->getId()}, {$dep}, '{$func->getFuncao()}')");
                }
            }
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        } catch (Exception $e) {
            print "Erro: " . $e->getMessage() . "\n";
        }

        return $result;


        /* Create a new mysqli funcect with database connection parameters */
        /* include_once('Conexao.class.php');
          $con = new Conexao();
          $mysqli = $con->conectar();
          try{
          $mysqli->exec("Insert into funcionarios values('1','1',1,'1','1','1','1','1','1','true')");
          }catch(PDOException $e){
          echo "afe";
          print $e->getMessage();
          }
          /* Create a prepared statement */
        /* if($stmt = $mysqli->prepare("Insert into funcionarios values(default,?,?,?,?,?,?,?,?,?,?)")) {

          /*preencho os campos
          s - string, b - boolean, i - int, etc */
        /* 		$stmt->bind_param("ss",$func->getNome(),$func->getCpf());
          $stmt->bind_param("is",$func->getRg(),$func->getEndereco());
          $stmt->bind_param("ss",$func->getLogin(),$func->getSenha());
          $stmt->bind_param("ss",$func->getTelefone1(),$func->getTelefone2());
          $stmt->bind_param("b",$func->getAdministrador());

          //Executa
          $stmt -> execute();
          echo "OK";
          }else{
          echo "afe";
          } */
        /* Close connection */
        //$mysqli -> close();
    }

    public function deletar($func) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->exec("Delete from funcionario where idFuncionario = {$func->getId()}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }

        return $result;
    }

    public function alterar($func) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->exec("Update funcionario set nome = '{$func->getNome()}', cpf = '{$func->getCpf()}', rg ='{$func->getRg()}', endereco = '{$func->getEndereco()}', login = '{$func->getLogin()}', senha = '{$func->getSenha()}', telefone1 = '{$func->getTelefone1()}', telefone2 = '{$func->getTelefone2()}', administrador = {$func->getAdministrador()}, gerente = {$func->getGerente()}, agente = {$func->getAgente()} where idFuncionario = {$func->getId()}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

   

    public function buscarCpf($cpf) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario where cpf = {$cpf}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarNome($nome) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario where nome like '%{$nome}%'");
    
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarLogin($login) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario where login like '{$login}'");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }
    
    public function verificarDepartamento($id,$dep) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario_has_departamento where idFuncionario = {$id} AND idDepartamento = {$dep}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
      
        return $result;
    }



    public function buscarTodos() {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function buscarId($id) {
        try {
            //Executa a instrua��o sql
            $result = $this->conn->query("Select * from funcionario where idFuncionario = {$id}");
        } catch (PDOException $e) {
            print "Erro!: " . $e->getMessage() . "\n";
            die();
        }
        return $result;
    }

    public function __destruct() {
        //fecha a conex�o
        $this->conn = null;
    }

}
?>